package com.fang.dubbo.mock.rpc.register;


import com.fang.dubbo.mock.common.URL;
import com.fang.dubbo.mock.rpc.annotation.FangDubboService;
import com.fang.dubbo.mock.rpc.provider.DemoRpcServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/1/2 3:34 下午
 **/
public class RemoteMapRegister {
    private static final Map<String, List<URL>> REGISTER_MAP = new ConcurrentHashMap<>(16);
    private static final Map<String, Class<?>> IMPL_MAP = new ConcurrentHashMap<>(16);

    public static void register() {
        scannerClass();
    }

    public static List<URL> getRegister(String interfaceName) {
        return REGISTER_MAP.get(interfaceName);
    }

    public static Class<?> getImpl(String interfaceName) {
        return IMPL_MAP.get(interfaceName);
    }

    public static boolean containsKey(String interfaceName) {
        return REGISTER_MAP.containsKey(interfaceName);
    }

    private static void scannerClass() {
        // 模拟scan
        FangDubboService annotation = DemoRpcServiceImpl.class.getAnnotation(FangDubboService.class);
        List<URL> list = new ArrayList<>();
        list.add(new URL(annotation.ip(), annotation.port()));
        REGISTER_MAP.put(annotation.clazz().getName(), list);
        IMPL_MAP.put(annotation.clazz().getName(), DemoRpcServiceImpl.class);
    }
}
